package com.bolingcavalry.crm.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.product.mapper.ProductMapper;
import com.bolingcavalry.crm.product.service.ProductService;
import com.dto.ProductDTO;
import com.entity.product.Product;
import com.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;


    @Override
    public PageResult<ProductVO> getProductsByConditions(Integer pageNum, Integer pageSize, String name, String version, String specification,String problemCategory) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.like("name", name); // 模糊匹配
        }
        if (StringUtils.hasText(version)) {
            queryWrapper.eq("version", version); // 精确匹配
        }
        if (StringUtils.hasText(specification)) {
            queryWrapper.eq("specification", specification); // 精确匹配
        }
        if (StringUtils.hasText(problemCategory)) {
            queryWrapper.eq("problem_category", problemCategory); // 精确匹配
        }

        IPage<Product> resultPage;
        if (pageSize == null) {
            // 查询全部数据，使用足够大的页面大小
            Page<Product> page = new Page<>(1, 100000);
            resultPage = productMapper.selectPage(page, queryWrapper);

            // 调整分页信息，使其更符合"查询全部"的语义
            resultPage.setPages(1);
            resultPage.setCurrent(1);
            resultPage.setSize(resultPage.getTotal());
        } else {
            // 正常分页查询
            Page<Product> page = new Page<>(pageNum, pageSize);
            resultPage = productMapper.selectPage(page, queryWrapper);
        }

        List<ProductVO> voList = resultPage.getRecords().stream()
                .map(this::convertToVO)
                .toList();

        PageResult<ProductVO> result = new PageResult<>();
        result.setTotal(resultPage.getTotal());
        result.setPages((int) resultPage.getPages());
        result.setCurrent((int) resultPage.getCurrent());
        result.setSize((int) resultPage.getSize());
        result.setRecords(voList);

        return result;
    }

    //get查询产品
    @Override
    public ProductVO getProductById(Long id) {
        Product product = productMapper.selectById(id);
        return convertToVO(product);
    }

    //新增产品
    @Override
    public ResponseResult createProduct(ProductDTO productDTO) {
        if(productDTO.getName().isEmpty()){
            return ResponseResult.error("产品名称不能为空");
        }
        // 1. 校验产品名称是否已存在
        Product existingProduct = productMapper.selectOne(new QueryWrapper<Product>()
                .eq("name", productDTO.getName()));
        if (existingProduct != null) {
            return ResponseResult.error("产品名称已存在");
        }
        Product product = new Product();
        BeanUtils.copyProperties(productDTO,product);
        if(productMapper.insert(product)>0)
        return ResponseResult.success("新增成功");
        else {throw new RuntimeException("新增失败");}
    }

    //更新产品
    @Override
    public Boolean updateProduct(ProductDTO productDTO) {
        Product product = productMapper.selectById(productDTO.getId());
        if (product == null) {
            return false;
        }
        BeanUtils.copyProperties(productDTO,product);
        if(productMapper.updateById(product)>0)
        return true;
        else {throw new RuntimeException("更新失败");}
    }

    //删除产品
    @Override
    public void deleteProduct(Long id) {
        productMapper.deleteById(id);
    }

    //通用转换方法
    private ProductVO convertToVO(Product product) {
        ProductVO vo = new ProductVO();
        BeanUtils.copyProperties(product,vo);
        return vo;
    }
}